

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>dscribe.descriptors.coulombmatrix &mdash; DScribe 0.2.9 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/style.css" type="text/css" />
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.2.9
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../doc/modules.html">Documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../contributing.html">Contributing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../citing.html">Citing DScribe</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../about.html">About</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">DScribe</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>dscribe.descriptors.coulombmatrix</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for dscribe.descriptors.coulombmatrix</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;Copyright 2019 DScribe developers</span>

<span class="sd">Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="sd">you may not use this file except in compliance with the License.</span>
<span class="sd">You may obtain a copy of the License at</span>

<span class="sd">    http://www.apache.org/licenses/LICENSE-2.0</span>

<span class="sd">Unless required by applicable law or agreed to in writing, software</span>
<span class="sd">distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="sd">WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="sd">See the License for the specific language governing permissions and</span>
<span class="sd">limitations under the License.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">ase</span> <span class="k">import</span> <span class="n">Atoms</span>

<span class="kn">from</span> <span class="nn">dscribe.core</span> <span class="k">import</span> <span class="n">System</span>
<span class="kn">from</span> <span class="nn">dscribe.descriptors.matrixdescriptor</span> <span class="k">import</span> <span class="n">MatrixDescriptor</span>


<div class="viewcode-block" id="CoulombMatrix"><a class="viewcode-back" href="../../../doc/dscribe.descriptors.html#dscribe.descriptors.coulombmatrix.CoulombMatrix">[docs]</a><span class="k">class</span> <span class="nc">CoulombMatrix</span><span class="p">(</span><span class="n">MatrixDescriptor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Calculates the zero padded Coulomb matrix.</span>

<span class="sd">    The Coulomb matrix is defined as:</span>

<span class="sd">        C_ij = 0.5 Zi**exponent      | i = j</span>
<span class="sd">             = (Zi*Zj)/(Ri-Rj)	     | i != j</span>

<span class="sd">    The matrix is padded with invisible atoms, which means that the matrix is</span>
<span class="sd">    padded with zeros until the maximum allowed size defined by n_max_atoms is</span>
<span class="sd">    reached.</span>

<span class="sd">    To reach invariance against permutation of atoms, specify a valid option</span>
<span class="sd">    for the permutation parameter.</span>

<span class="sd">    For reference, see:</span>
<span class="sd">        &quot;Fast and Accurate Modeling of Molecular Atomization Energies with</span>
<span class="sd">        Machine Learning&quot;, Matthias Rupp, Alexandre Tkatchenko, Klaus-Robert</span>
<span class="sd">        Mueller, and O.  Anatole von Lilienfeld, Phys. Rev. Lett, (2012),</span>
<span class="sd">        https://doi.org/10.1103/PhysRevLett.108.058301</span>
<span class="sd">    and</span>
<span class="sd">        &quot;Learning Invariant Representations of Molecules for Atomization Energy</span>
<span class="sd">        Prediction&quot;, Gregoire Montavon et. al, Advances in Neural Information</span>
<span class="sd">        Processing Systems 25 (NIPS 2012)</span>
<span class="sd">    &quot;&quot;&quot;</span>
<div class="viewcode-block" id="CoulombMatrix.create"><a class="viewcode-back" href="../../../tutorials/coulomb_matrix.html#dscribe.descriptors.coulombmatrix.CoulombMatrix.create">[docs]</a>    <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">n_jobs</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the Coulomb matrix for the given systems.</span>

<span class="sd">        Args:</span>
<span class="sd">            system (:class:`ase.Atoms` or list of :class:`ase.Atoms`): One or</span>
<span class="sd">                many atomic structures.</span>
<span class="sd">            n_jobs (int): Number of parallel jobs to instantiate. Parallellizes</span>
<span class="sd">                the calculation across samples. Defaults to serial calculation</span>
<span class="sd">                with n_jobs=1.</span>
<span class="sd">            verbose(bool): Controls whether to print the progress of each job</span>
<span class="sd">                into to the console.</span>

<span class="sd">        Returns:</span>
<span class="sd">            np.ndarray | scipy.sparse.csr_matrix: Coulomb matrix for the given</span>
<span class="sd">            systems. The return type depends on the &#39;sparse&#39; and</span>
<span class="sd">            &#39;flatten&#39;-attributes. For flattened output a single numpy array or</span>
<span class="sd">            sparse scipy.csr_matrix is returned. The first dimension is</span>
<span class="sd">            determined by the amount of systems.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If single system given, skip the parallelization</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">system</span><span class="p">,</span> <span class="p">(</span><span class="n">Atoms</span><span class="p">,</span> <span class="n">System</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_single</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>

        <span class="c1"># Combine input arguments</span>
        <span class="n">inp</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i_sys</span><span class="p">,)</span> <span class="k">for</span> <span class="n">i_sys</span> <span class="ow">in</span> <span class="n">system</span><span class="p">]</span>

        <span class="c1"># Here we precalculate the size for each job to preallocate memory.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flatten</span><span class="p">:</span>
            <span class="n">n_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">system</span><span class="p">)</span>
            <span class="n">k</span><span class="p">,</span> <span class="n">m</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">n_samples</span><span class="p">,</span> <span class="n">n_jobs</span><span class="p">)</span>
            <span class="n">jobs</span> <span class="o">=</span> <span class="p">(</span><span class="n">inp</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">m</span><span class="p">):(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">k</span> <span class="o">+</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">m</span><span class="p">)]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_jobs</span><span class="p">))</span>
            <span class="n">output_sizes</span> <span class="o">=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">job</span><span class="p">)</span> <span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">output_sizes</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Create in parallel</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_parallel</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_single</span><span class="p">,</span> <span class="n">n_jobs</span><span class="p">,</span> <span class="n">output_sizes</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">output</span></div>

<div class="viewcode-block" id="CoulombMatrix.get_matrix"><a class="viewcode-back" href="../../../doc/dscribe.descriptors.html#dscribe.descriptors.coulombmatrix.CoulombMatrix.get_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Creates the Coulomb matrix for the given system.</span>

<span class="sd">        Args:</span>
<span class="sd">            system (:class:`ase.Atoms` | :class:`.System`): Input system.</span>

<span class="sd">        Returns:</span>
<span class="sd">            np.ndarray: Coulomb matrix as a 2D array.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Make sure that the system is non-periodic</span>
        <span class="n">system</span><span class="o">.</span><span class="n">set_pbc</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>

        <span class="c1"># Calculate offdiagonals</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_atomic_numbers</span><span class="p">()</span>
        <span class="n">qiqj</span> <span class="o">=</span> <span class="n">q</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span><span class="o">*</span><span class="n">q</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">]</span>
        <span class="n">idmat</span> <span class="o">=</span> <span class="n">system</span><span class="o">.</span><span class="n">get_inverse_distance_matrix</span><span class="p">()</span>
        <span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">idmat</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">cmat</span> <span class="o">=</span> <span class="n">qiqj</span><span class="o">*</span><span class="n">idmat</span>

        <span class="c1"># Set diagonal</span>
        <span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">cmat</span><span class="p">,</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">q</span> <span class="o">**</span> <span class="mf">2.4</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">cmat</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  
  <div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      <span class="fa fa-book"> Versions</span>
      v: 0.2.9
      <span class="fa fa-caret-down"></span>
    </span>
    <div class="rst-other-versions">
      <dl>
        <dt>Versions</dt>
        
          <dd><a href="https://singroup.github.io/dscribe/dev">0.3.0a0 (development)</a></dd>
        
          <dd><a href="https://singroup.github.io/dscribe">0.2.9 (latest stable)</a></dd>
        
      </dl>
    </div>
  </div>


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>